package com.andy.nature.ui.activity

import android.content.Intent
import android.graphics.Color
import android.graphics.drawable.AnimationDrawable
import android.view.Gravity
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import androidx.fragment.app.DialogFragment
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.andy.common.base.BaseVMActivity
import com.andy.common.extend.setOnIMClickListener
import com.andy.common.extend.showHighText
import com.andy.dialog.showDialog
import com.andy.logcat.LogUtils
import com.andy.nature.R
import com.andy.nature.databinding.ActivityTestBinding
import com.andy.nature.databinding.DialogBottomListBinding
import com.andy.nature.databinding.DialogBottomListTitleBinding
import com.andy.nature.databinding.DialogCustomLayoutBinding
import com.andy.nature.databinding.DialogInputLayoutBinding
import com.andy.nature.databinding.DialogLoadingLayoutBinding
import com.andy.nature.databinding.DialogProgressLayoutBinding
import com.andy.nature.ui.adapter.DialogBottomAdapter
import com.andy.nature.vm.CommonViewModel
import com.andy.permission.constants.Constants
import com.andy.permission.utils.PermissionUtils
import com.andy.toast.extend.showErrorToast
import com.andy.toast.extend.showNormalToast
import com.andy.toast.extend.showSuccessToast
import com.hjq.permissions.XXPermissions

/**
 * Author:Andy
 * Date:2023/3/20 17:51
 * Description:TestActivity
 */
class UseActivity : BaseVMActivity<ActivityTestBinding, CommonViewModel>() {

    override fun viewModelClass(): Class<CommonViewModel> {
        return CommonViewModel::class.java
    }

    override fun getViewBinding(): ActivityTestBinding {
        return ActivityTestBinding.inflate(layoutInflater)
    }

    override fun initObserve() {

    }

    override fun initData() {
        mBinding?.apply {
            val content = "Wow hello, My name is Jack,Hello what's your name ,Wow my name is Jack too,Nice to meet you"
            val color = Color.parseColor("#1884F0")
            tvHighLight.showHighText(content, "hello", "Jack", color = color)
        }
    }

    override fun initAction() {
        mBinding?.run {
            vTitle.setClickLeftCallBack {
                finish()
            }
            btnToastNormal.setOnIMClickListener {
                "这是正常的吐司".showNormalToast()
            }
            btnToastSuccess.setOnIMClickListener {
                "这是成功的吐司".showSuccessToast()
            }
            btnToastError.setOnIMClickListener {
                "这是失败的吐司".showErrorToast()
            }
            btnLogV.setOnIMClickListener {
                LogUtils.v("输出V类型日志")
            }
            btnLogD.setOnIMClickListener {
                LogUtils.d("输出D类型日志")
            }
            btnLogI.setOnIMClickListener {
                LogUtils.i("输出I类型日志")
            }
            btnLogW.setOnIMClickListener {
                LogUtils.w("输出W类型日志")
            }
            btnLogE.setOnIMClickListener {
                LogUtils.e("输出E类型日志")
            }
            btnLogWtf.setOnIMClickListener {
                LogUtils.wtf("输出WTF类型日志")
            }
            btnPermissionCamera.setOnIMClickListener {
                PermissionUtils.checkAndRequestPermission(this@UseActivity, Constants.CAMERA_PERMISSION) { isGranted, isNotAskAgain ->
                    if (isGranted) {
                        "相机权限允许了".showSuccessToast()
                    } else {
                        if (isNotAskAgain) {
                            "相机权限永久拒绝".showErrorToast()
                        } else {
                            "相机权限临时拒绝".showErrorToast()
                        }
                    }
                }
            }
            btnJumpSetting.setOnIMClickListener {
                if (XXPermissions.isPermanentDenied(this@UseActivity, Constants.CAMERA_PERMISSION)) {
                    PermissionUtils.jumpAndStartPermission(this@UseActivity, Constants.CAMERA_PERMISSION)
                }
            }
            btnDialog1.setOnIMClickListener {
                showDialog {
                    addLayout(R.layout.dialog_custom_layout)
                    setDialogCancelable(false)
                    bind<DialogCustomLayoutBinding> {
                        it.dialogConfirm.setOnIMClickListener {
                            "确定".showNormalToast()
                            dismiss()
                        }
                    }
                }
            }
            btnDialog2.setOnIMClickListener {
                showDialog {
                    addLayout(R.layout.dialog_custom_layout)
                    bind<DialogCustomLayoutBinding> {
                        it.dialogCancel.isVisible = true
                        it.dialogButtonLine.isVisible = true
                        it.dialogConfirm.setOnIMClickListener {
                            "确认".showNormalToast()
                            dismiss()
                        }
                        it.dialogCancel.setOnIMClickListener {
                            "取消".showNormalToast()
                            dismiss()
                        }
                    }
                }
            }
            btnDialog3.setOnIMClickListener {
                showDialog {
                    addLayout(R.layout.dialog_input_layout)
                    bind<DialogInputLayoutBinding> {
                        it.tvSubmit.setOnIMClickListener { view ->
                            val name = it.etName.text.toString()
                            val password = it.etPass.text.toString()
                            if (name.isEmpty()) {
                                "请输入账号".showNormalToast()
                                return@setOnIMClickListener
                            }
                            if (password.isEmpty()) {
                                "请输入密码".showNormalToast()
                                return@setOnIMClickListener
                            }
                            "登录成功".showSuccessToast()
                            dismiss()
                        }
                    }
                }
            }
            btnDialog4.setOnIMClickListener {
                showDialog {
                    addLayout(R.layout.dialog_loading_default)
                }
            }
            btnDialog5.setOnIMClickListener {
                showDialog {
                    addLayout(R.layout.dialog_progress_layout)
                    bind<DialogProgressLayoutBinding> {
                        it.progressBar.max = 100
                        it.progressBar.progress = 66
                        val progress = it.progressBar.progress * 100 / it.progressBar.max
                        it.tvContent.text = "${progress}%"
                    }
                }
            }
            btnDialog6.setOnIMClickListener {
                showDialog {
                    addLayout(R.layout.dialog_loading_layout)
                    setDialogCancelable(true)
                    style {
                        setStyle(DialogFragment.STYLE_NO_FRAME, com.andy.dialog.R.style.AppDialog)
                        bind<DialogLoadingLayoutBinding> {
                            val anim = it.image.background as AnimationDrawable
                            it.image.post {
                                anim.start()
                            }
                        }
                    }
                }
            }
            btnDialog7.setOnIMClickListener {
                showDialog {
                    addLayout(R.layout.dialog_bottom_list)
                    bind<DialogBottomListBinding> {
                        val adapter = DialogBottomAdapter()
                        it.dialogRecycler.layoutManager = LinearLayoutManager(this.context)
                        it.dialogRecycler.addItemDecoration(DividerItemDecoration(this.context, DividerItemDecoration.VERTICAL))
                        it.dialogRecycler.adapter = adapter
                        adapter.setList(mViewModel.getTestList())
                        it.tvCancel.setOnIMClickListener {
                            dismiss()
                        }
                    }
                    style {
                        dialog?.window?.setGravity(Gravity.BOTTOM)
                        dialog?.window?.setWindowAnimations(com.andy.dialog.R.style.DialogBaseAnimation)
                    }
                }
            }
            btnDialog8.setOnIMClickListener {
                showDialog {
                    addLayout(R.layout.dialog_bottom_list_title)
                    bind<DialogBottomListTitleBinding> {
                        val adapter = DialogBottomAdapter()
                        it.dialogRecycler.layoutManager = LinearLayoutManager(this.context)
                        val divider = DividerItemDecoration(this.context, DividerItemDecoration.VERTICAL)
                        divider.setDrawable(ContextCompat.getDrawable(this@UseActivity, R.drawable.divider_horizontal_line)!!)
                        it.dialogRecycler.addItemDecoration(divider)
                        it.dialogRecycler.adapter = adapter
                        adapter.setList(mViewModel.getTestList())
                        it.tvCancel.setOnIMClickListener {
                            dismiss()
                        }
                    }
                    style {
                        dialog?.window?.setGravity(Gravity.BOTTOM)
                        dialog?.window?.setWindowAnimations(com.andy.dialog.R.style.DialogBaseAnimation)
                    }
                }
            }

            btnScan.setOnIMClickListener {
                PermissionUtils.checkAndRequestPermission(this@UseActivity, Constants.CAMERA_PERMISSION) { isGranted, isNotAskAgain ->
                    if (isGranted) {
                        startActivity(ScanCodeActivity::class.java)
                    } else {
                        if (isNotAskAgain) {
                            "相机权限永久拒绝".showErrorToast()
                        } else {
                            "相机权限临时拒绝".showErrorToast()
                        }
                    }
                }
            }
            btnQrcode.setOnIMClickListener {
                startActivity(Intent(this@UseActivity, QrCodeActivity::class.java))
            }
            highLightView.setOnIMClickListener {
                highLightView.shake()
            }
        }
    }

    override fun requestError(it: Exception) {
        super.requestError(it)
        LogUtils.e("------请求报异常------${it.message}")
    }

    override fun requestFinally(it: Int) {
        super.requestFinally(it)
        LogUtils.e("------请求结束------")
    }
}